import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

// 选项式 创建 store，并暴露出去
// 参数一：名字，必填值且唯一
// 参数二：书写方式采用对象形式
export const useStore = defineStore('main', {
  // state: () => {
  //   return { count: 0 }
  // },
  state: () => ({ counter: 0 }),
  getters: {
    doubleCount: () => this.counter * 2,
    doubleCountPlusOne() {
      return this.counter + 1
    },
  },
  actions: {
    increment() {
      this.counter++
    },
  },
})

export const useCounterStore = defineStore('counter', {})
export const useUserStore = defineStore('user', () => {
  const age = ref(27)
  const account = ref(1)

  const doubleCount = computed(() => {
    return account.value * 2
  })

  function increment() {
    account.value++
  }

  return { age, account, doubleCount, increment } // 将数据暴露出去，共享给需要的组件
})
